package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.LikeAdapter;
import flc.ast.databinding.ActivitySearchBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SearchActivity extends BaseAc<ActivitySearchBinding> {

    public static String movieKey;
    private List<StkResBean> listShow = new ArrayList<>();
    private LikeAdapter movieAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivMovieSearch.setOnClickListener(this);
        mDataBinding.ivClear.setOnClickListener(this);
        mDataBinding.etMovieKey.setText(movieKey);
        mDataBinding.rvSearchList.setLayoutManager(new GridLayoutManager(mContext, 3));
        movieAdapter = new LikeAdapter();
        movieAdapter.setShow(true);
        mDataBinding.rvSearchList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);
        mDataBinding.etMovieKey.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                movieKey = mDataBinding.etMovieKey.getText().toString().trim();
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    if (movieKey.isEmpty()) {
                        ToastUtils.showShort(getString(R.string.please_input_search));
                        return true;
                    }
                    getData();
                }
                return false;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivMovieSearch://搜索
                movieKey = mDataBinding.etMovieKey.getText().toString().trim();
                if (movieKey.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                getData();
                break;
            case R.id.ivClear://清空
                movieKey = "";
                mDataBinding.etMovieKey.setText(movieKey);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        saveRecord(bean);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        StkResApi.getTagResourceSetsList(null, AppConfig.SEARCH_DATA, movieKey, null, false, new IReqRetCallback<StkResSetBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkResSetBean result) {
                if (success) {
                    if (result.articleList.size() > 0) {
                        listShow.addAll(result.articleList);
                    }
                }
                mDataBinding.tvSearchNum.setText(getString(R.string.search_num, listShow.size()));
                if (listShow.size() > 0) {
                    movieAdapter.setList(listShow);
                    mDataBinding.rvSearchList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSearchNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvSearchList.setVisibility(View.GONE);
                    mDataBinding.ivSearchNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void saveRecord(StkResBean bean) {
        List<StkResBean> listSave = new ArrayList<>();
        listSave.add(bean);
        List<StkResBean> listSp = AppSpUtils.getReadRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveReadRecordList(listSp);
        } else {
            AppSpUtils.saveReadRecordList(listSave);
        }
    }

}